/* 登录 */
/*function login() {
	$.ajax({
		url : "/Wchat/functionalBusinessController/login.do?token="+localStorage.token,
		success : function(json) {
			//将登录成功之后的token存入客户端缓存
			json=$.parseJSON(json);
			//localStorage.v8sharetoken=json.token;
		}
	});
}*/
function onBridgeReady() {
	
	WeixinJSBridge.invoke('getBrandWCPayRequest', {
		"appId" : payInfo.appId, // 公众号名称，由商户传入
		"timeStamp" : payInfo.timeStamp, // 时间戳，自1970年以来的秒数
		"nonceStr" : payInfo.nonceStr, // 随机串
		"package" : payInfo.package,
		"signType" : "MD5", // 微信签名方式：
		"paySign" : payInfo.paySign
	// 微信签名
	}, function(res) {

		if (res.err_msg == "get_brand_wcpay_request:ok") { // 使用以上方式判断前端返回,微信团队郑重提示：res.err_msg将在用户支付成功后返回
															// ok，但并不保证它绝对可靠。
			window.location.href = "infoCenter.html?orderId=";
		}
	});
	
}

// 获取订单详情
function getTripDetail() {
	$.ajax({
		url : "/Wchat/functionalBusinessController/getTripDetail.do",
		success : function(json) {
			console.log(json);
			json = $.parseJSON(json);// 将字字符串转换成JS对象
			var li_element = $(".data-list").eq(0).find("li").eq(0);// 获取数据模型
			var startAddress = json.startAddress;// 起始点
			var endAddress = json.endAddress;// 结束地址
			var tel = json.driverInfo.account;
			var orderId = json.orderId;// 订单号
			$(li_element).find(".startAddress").eq(0).text(startAddress);
			$(li_element).find(".endAddress").eq(0).text(endAddress);
			$(li_element).find(".driverTel").eq(0).attr("href", "tel:" + tel);

		}
	});
}

// 轮询服务端,获取接单的司机
function getTrip() {
	jishiqi = setInterval(function() {
		$.ajax({
			url : "/Wchat/functionalBusinessController/getTrip.do?tripId="
					+ localStorage.tripId_p,
			success : function(json) {
				if (json.status == 1) {
					// closeTimer(jishiqi);
					var trip_status = json.tripStatus;// 行程状态

					if (tripStatus != trip_status) {

						var msg = "";

						if (trip_status == "new") {

						}

						if (trip_status == "ordered") {
							msg = "已经有司机接了您的行程订单";
						}

						if (trip_status == "started") {
							msg = "司机正向您的方向驶来.";
						}

						if (trip_status == "pickuped") {
							msg = "司机已经街接到您.";
						}

						if (trip_status == "done") {
							msg = "您已安全到达.";
						}

						if (trip_status == "cancelled") {
							msg = "哦，天呐，那可恶的司机竟然放您鸽子,赶快去举报他！！！";
						}

						$.noti({
							title : "行程状态！",
							text : msg,
							media : "<img src='./images/present.png' />",
							data : {
								message : "逗你玩的!"
							},
							time : 3000,
							onClick : function(data) {
								$.alert(data.message);
							}
						});
					}

					tripStatus = trip_status;

				}
			}
		});
	}, 2000);
}

var pageIndex = 0;
var pageSize = 3;
// 获取我的订单列表
function getMyTrips() {
	var uri = "/Wchat/functionalBusinessController/getMyTrips.do?pageIndex="
			+ pageIndex + "&pageSize=" + pageSize;
	$
			.ajax({
				url : uri,
				success : function(json) {

					// console.log(json);
					if (json.success == null || json.success == undefined) {

						// 删除加载更多操作
						$(".getMore").remove();

						json = $.parseJSON(json);// 将字字符串转换成JS对象
						var li_element = $(".data-list").eq(0).find("li").eq(0);// 获取数据模型
						var str = "";
						for (var i = 0; i < json.length; i++) {
							var startAddress = json[i].startAddress;// 起始点
							var endAddress = json[i].endAddress;// 结束地址
							
							// 订单创建时间
							var newDate = new Date();
							newDate.setTime(json[i].createDatetime);
							var createDatetime = newDate
									.format('yyyy-MM-dd h:m');

							// 订单状态
							var orderStatus = formatOrderStatus(json[i].status);

							// 支付状态
							var payStatus = "";
	
							if (json[i].payStatus == "WAIT_PAY") {
								
								payStatus = "<button userId='"+ json[i].userId +"' orderId='"
								+ json[i].orderId
								+ "' finalCost='"
								+ json[i].estimateCost
								+ "' class=\"v8-btn pay_money\">确认支付</button>";

							}else if(json[i].payStatus=="NOT_NEED_PAY"){
								
								payStatus = "<span>已支付</span>";
							
							}else{
								
							}
							
							
							// 司机信息
							var driverInfo = json[i].driverInfo;
							
							var driverName = "";
							
							if (driverInfo != null) {

								if(driverName!=null){
									driverName = driverInfo.firstName+"师傅";//司机姓名
								}
								
								var tel = "";//	司机电话
								if (json[i].driverInfo != null) {
									tel = json[i].driverInfo.account;
								}
							}

							str += "<li>";

							str += "<div>" + "	<div class='infoheader'>"
									+ "		<div class='info-date'>"
									+ createDatetime
									+ "</div>"
									+ "		<div class='info-status' style='color: #7cbdff;'>"
									+ orderStatus
									+ "</div>"
									+ "		<div class='clear-float'></div>"
									+ "	</div>"
									+ "	<div class='info-content'>"
									+ "		<input type='hidden' value='"
									+ json[i].id
									+ "' class='orderId'>"
									+ "		<p>"
									+ "			上车地点 : <span class='startAddress'>"
									+ startAddress
									+ "</span>"
									+ "		</p>"
									+ "		<p>"
									+ "			下车地点 : <span class='endAddress'>"
									+ endAddress
									+ "</span>"
									+ "		</p>"
									+ "		<p>"
									+ "			预计耗时 : <span>45分钟</span>"
									+ "		</p>"
									+ "		<p>"
									+ "			&nbsp;&nbsp;车牌 : <span>沪A28570</span>"
									+ "		</p>"
									+ "		<p>"
									+ "			&nbsp;&nbsp;司机 : <span>"
									+ driverName
									+ "</span> <a class='driverTel'"
									+ "				href='tel:"
									+ tel
									+ "'><img src='img/dianhua copy 2@3x.png'"
									+ "				width='15'></a>"
									+ "		</p>"
									+ "	</div>"
									+ "	<div class='info-foot'>"
									+ "		<div class='info-money'>"
									+ "			拼车费用:<span class='pinche-feiyong'>￥"
									+ json[i].estimateCost
									+ "</span>"
									+ "		</div>"
									+ "		<div class='info-payment'>"
									+ payStatus
									+ "			"
									+ "		</div>"
									+ "		<div class='clear-float'></div>"
									+ "	</div>" + "</div>";

							str += "</li>";

						}
						if (json.length >= 3) {
							str = str
									+ "<li class='getMore' id='getMore' style='text-align:center;'><a>加载更多...</a></li>";
						}

						/* $("#data-list").empty(); */
						$("#data-list").append(str);
						$.hideLoading();

					} else if (json.success == false) {
						// 未登录,或其他错误
						alert(json.msg);
					}
				}
			});
}

function formatOrderStatus(status) {
	var status_cn = "";
	switch (status) {
	case "new":
		status_cn = "<span style='color:green;'>新订单</span>";
		break;
	case "ordered":
		status_cn = "已送达";
		break;
	case "started":
		status_cn = "进行中";
		break;
	case "pickuped":
		status_cn = "接到乘客";
		break;
	case "done":
		status_cn = "<span style='color:black;'>已结束</span>";
		break;
	case "cancelled":
		status_cn = "<span style='color:#ccc;'>已取消</span>";
		break;
	}

	return status_cn;
}
